#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "cadredeuxdeux.h"
#include "imagecadre.h"
#include "image.h"
#include "cadreundeux.h"
#include "cadreununvertical.h"
#include "cadreununhorizontal.h"
#include <QFileDialog>
#include <QSignalMapper>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),_ecran(new ImageFond()),_cadre(NULL),_preValue(100)
{
    ui->setupUi(this);
    initialiser();
}

MainWindow::~MainWindow()
{
    delete ui;
    if(_ecran!=NULL)
        delete _ecran;
    if(_cadre!=NULL)
        delete _cadre;
}

void MainWindow::on_actionEnregistrer_activated()
{
    QString fichier  = QFileDialog::getSaveFileName(this, "Ouvrir un fichier", QString(), "Images (*.png *.gif *.jpg *.jpeg)");
    if(!fichier.isEmpty())
    {
        QPixmap image(QPixmap::grabWindow(_ecran->winId()));
        image.save(fichier);
    }
}

void MainWindow::zoomImage(int valeur)
{
    Case* c = NULL;
    Image * img = NULL;
    if(_ecran->selection)
        img = _ecran;
     else if((c=selectCase())!=NULL)
        img = &c->image;
    if(img!=NULL)
    {
        double val;
        if(valeur>0)
            val = 1.25;
        else val = 0.85;
        img->zoom(val);
        updateMiniature(NULL);
    }
}

void MainWindow::zoomCase(int valeur)
{
    Case *c;
    if((c = selectCase())!=NULL)
    {
        double val;
        if(valeur>0)
            val = 1.25;
        else val = 0.85;
        c->resize(c->size()*val);
    }
}

void MainWindow::choixCadre(int choix)
{
    if(_cadre!=NULL)
    {
        delete _cadre;
        _cadre = NULL;
    }
    switch(choix)
    {
     case 0:
        _cadre = new CadreDeuxDeux();
        break;
    case 1:
        _cadre = new CadreUnUnHorizontal();
        break;
    case 2:
        _cadre = new CadreUnUnVertical();
        break;
    case 3:
        _cadre = new CadreUnDeux();
        break;
    }
    if(_cadre!=NULL)
        _ecran->setLayout(&_cadre->layout);
    connectSignalImage();
}

void MainWindow::initialiser()
{
    _cadre = new CadreDeuxDeux();
    _ecran->setParent(ui->frameEcran);
    _ecran->setLayout(&_cadre->layout);
    _ecran->setMaximumSize(ui->frameEcran->size());
    _ecran->setCadre(_cadre);

    listeCadre.append(new ImageCadre("ressources/imageDeuxDeux.JPG",0));
    listeCadre.append(new ImageCadre("ressources/imageUnUnHorizontal.JPG",1));
    listeCadre.append(new ImageCadre("ressources/imageUnUnVertical.JPG",2));
    listeCadre.append(new ImageCadre("ressources/imageUnDeux.JPG",3));

    connectSignalImage();

    connect(_ecran,SIGNAL(mouseReleased(QMouseEvent *)),this,SLOT(updateMiniature(QMouseEvent*)));

    QSignalMapper *signalMapper = new QSignalMapper(this);
    connect(signalMapper, SIGNAL(mapped(int)), this, SLOT(zoomImage(int)));
    signalMapper->setMapping(ui->buttonZoomIn, 1);
    signalMapper->setMapping(ui->buttonZoomOut, -1);
    connect(ui->buttonZoomIn, SIGNAL(clicked()), signalMapper, SLOT(map()));
    connect(ui->buttonZoomOut, SIGNAL(clicked()), signalMapper, SLOT(map()));

    QSignalMapper *signalMapper1 = new QSignalMapper(this);
    connect(signalMapper1, SIGNAL(mapped(int)), this, SLOT(zoomCase(int)));
    signalMapper1->setMapping(ui->buttonZPlus, 1);
    signalMapper1->setMapping(ui->buttonZMoins, -1);
    connect(ui->buttonZPlus, SIGNAL(clicked()), signalMapper1, SLOT(map()));
    connect(ui->buttonZMoins, SIGNAL(clicked()), signalMapper1, SLOT(map()));


    QSignalMapper *signalMapper2 = new QSignalMapper(this);
    connect(signalMapper2, SIGNAL(mapped(int)), this, SLOT(on_move_case(int)));
    signalMapper2->setMapping(ui->buttonLeft,0);
    signalMapper2->setMapping(ui->buttonRight,1);
    signalMapper2->setMapping(ui->buttonUp,2);
    signalMapper2->setMapping(ui->buttonDown,3);
    connect(ui->buttonLeft, SIGNAL(clicked()), signalMapper2, SLOT(map()));
    connect(ui->buttonRight, SIGNAL(clicked()), signalMapper2, SLOT(map()));
    connect(ui->buttonUp, SIGNAL(clicked()), signalMapper2, SLOT(map()));
    connect(ui->buttonDown, SIGNAL(clicked()), signalMapper2, SLOT(map()));



    for(int i=0; i<listeCadre.count();i++)
    {
        connect(listeCadre[i],SIGNAL(clicked(int)),this,SLOT(choixCadre(int)));
        ui->gridLayout->addWidget(listeCadre[i],i,0);
    }
}

void MainWindow::connectSignalImage()
{
    for(int i=0; i< _cadre->liste.count();i++)
        connect(&(_cadre->liste[i]->image),SIGNAL(mouseReleased(QMouseEvent *)),this,SLOT(updateMiniature(QMouseEvent*)));
}

Case *MainWindow::selectCase()
{
    foreach(Case* c, _cadre->liste)
    {
        if(c->image.selection)
        {
           return c;
        }
    }
    return NULL;
}

void MainWindow::on_actionQuitter_activated(int arg1)
{
    close();
}

void MainWindow::updateMiniature(QMouseEvent *event)
{
    ui->visualisation->setScaledContents(true);
    ui->visualisation->setPixmap(QPixmap::grabWindow(_ecran->winId()));
}

void MainWindow::on_move_case(int direct)
{
    Case *c;
    if((c = selectCase())!=NULL)
    {
        QPoint point;
        switch(direct)
        {
            case 0:
                point.setX(-1);
                break;
            case 1:
                point.setX(1);
                break;
             case 2:
                point.setY(-1);
                break;
            case 3:
               point.setY(1);
               break;
        }
        c->move(c->pos()+point);
    }
}

void MainWindow::on_actionNouveau_activated(int arg1)
{
    _ecran->resize(ui->frameEcran->size());
    _ecran->reinitialiser();
    choixCadre(0);
    _ecran->setCadre(_cadre);
    updateMiniature(NULL);
}
